package com.stylefeng.guns.modular.goods.controller;

import com.stylefeng.guns.common.CheckboxVo;
import com.stylefeng.guns.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.goods.model.Spec;
import com.stylefeng.guns.modular.goods.service.ISpecService;

import java.util.List;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-09-05 16:10:05
 */
@Controller
@RequestMapping("/spec")
public class SpecController extends BaseController {

    private String PREFIX = "/goods/spec/";

    @Autowired
    private ISpecService specService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "spec.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/spec_add")
    public String specAdd() {
        return PREFIX + "spec_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/spec_update/{specId}")
    public String specUpdate(@PathVariable Long specId, Model model) {
        Spec spec = specService.selectById(specId);
        model.addAttribute("item",spec);
        LogObjectHolder.me().set(spec);
        return PREFIX + "spec_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return specService.selectList(null);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Spec spec) {
        specService.insert(spec);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long specId) {
        specService.deleteById(specId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Spec spec) {
        specService.updateById(spec);
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{specId}")
    @ResponseBody
    public Object detail(@PathVariable("specId") Long specId) {
        return specService.selectById(specId);
    }

    @RequestMapping(value = "/checkbox")
    @ResponseBody
    public List<CheckboxVo> checkbox() {
        return specService.checkbox();
    }
}
